package com.gitee.huanminabc.utils_tools.file_tool.excel.easy;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils;
import com.gitee.huanminabc.jcommon.model.FeeRate;

import java.math.BigDecimal;
import java.text.ParseException;

/**
 * @program: monorepo
 * @description:  FeeRate 转换器
 * @author: huanmin
 * @create: 2025-01-02 17:28
 **/
public class EasyFeeRateConverter implements Converter<FeeRate> {

    @Override
    public FeeRate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                   GlobalConfiguration globalConfiguration) throws ParseException {
        //判断
        if (cellData.getStringValue() != null) {
            BigDecimal bigDecimal = NumberUtils.parseBigDecimal(cellData.getStringValue(), contentProperty);
            return FeeRate.of(bigDecimal);
        }
        if (cellData.getNumberValue() != null) {
            return FeeRate.of(cellData.getNumberValue());
        }
        return null;
    }

    //写入excel 一般都是百分比的不是小数
    @Override
    public WriteCellData<?> convertToExcelData(FeeRate value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        return NumberUtils.formatToCellDataString(value.intValue(), contentProperty);
    }
}
